package attack.snowman;

import java.awt.geom.Rectangle2D;

import org.newdawn.slick.Animation;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Image;
import org.newdawn.slick.geom.Rectangle;

import framework.BasicFighter;
import framework.Util;
import framework.movements.PropulsionMovement;
import attack.yefi.SwordStorm;

public class IcePillar extends SwordStorm {
	
	protected boolean forward;
	
	public IcePillar(BasicFighter f) {
		super(f);
		Image[] frames = new Image[5];
		frames[0] = this.spriteSheet.getSprite(1, 4);
		frames[1] = this.spriteSheet.getSprite(2, 4);
		frames[2] = this.spriteSheet.getSprite(3, 4);
		frames[3] = this.spriteSheet.getSprite(3, 4);
		frames[4] = this.spriteSheet.getSprite(3, 4);
		this.attackAnimation = new Animation(frames, 100);
		this.attackAnimationBackward = Util.getFlippedAnimation(this.attackAnimation, true, false);
		this.attackAnimation.setLooping(false);
		this.attackAnimationBackward.setLooping(false);
		frames[0] = this.spriteSheet.getSprite(4, 4);
		frames[1] = this.spriteSheet.getSprite(0, 5);
		frames[2] = this.spriteSheet.getSprite(1, 3);
		this.attackJumpAnimation = new Animation(frames, 50);
		this.attackJumpAnimationBackward = Util.getFlippedAnimation(this.attackJumpAnimation, true, false);
		this.attackJumpAnimation.setLooping(false);
		this.attackJumpAnimationBackward.setLooping(false);
		
		frames = new Image[3];
		frames[0] = this.spriteSheet.getSprite(4, 4);
		frames[1] = this.spriteSheet.getSprite(0, 5);
		frames[2] = this.spriteSheet.getSprite(1, 5);
		this.ball = new Animation(frames, 50);
		this.ball.setDuration(2, 150);
		this.ball.setLooping(false);
		this.ballBackward = Util.getFlippedAnimation(this.ball, true, false);
		this.ballBackward.setLooping(false);
		forward = false;
		this.interruptOnHit = false;
	}
		
	@Override
	public void trigger() {
		super.trigger();
		this.forward = getAttacker().isForward();
		int forwardInt = this.forward ? 1 : -1;
		setX((int) (getAttacker().getHitBox().getX() + 200 * getScale() * forwardInt));
	}

	@Override
	public void update(GameContainer gc, int delta) {
		super.update(gc, delta);
		if (forward) {
			setX((int) (getAttacker().getHitBox().getX() + 200 * getScale()));
		} else {
			setX((int) (getAttacker().getHitBox().getX() - 600 * getScale()));
		}
	}

	@Override
	public void onHit(BasicFighter f, Rectangle2D collision) {
		if (f.getCurrentAttack().isInterruptable()) {
		    f.getCurrentAttack().interrupt();
		}
		if (!hasHit) {
			//LinearMovement m = new LinearMovement(f, new Point(f.getX(), f.getY() - 200 * getScale()));
			//m.setDuration(300);
			//m.setStayTimeEnd(200);
			//f.move(m);
			
			PropulsionMovement m = new PropulsionMovement(f, 0, -1800);
			m.setDuration(500);
			f.move(m);

			//m.setStayTimeEnd(200);
			f.hit(getPower(), 500);
			f.setParrying(false);
		}
	}
	
	@Override
	public boolean isBlocking() {
		return isActive() && currentTime < 500;
	}
	
	@Override
	public Rectangle getHitBox() {
		int height = (int) (100 * getScale());
		int width = (int) (100 * getScale());
		Rectangle h = getAttacker().getHitBox();
		Rectangle hitBox;
		hitBox = null;
		if (completed && currentTime < 500) {
			if (this.currentBall == this.ball) {
				hitBox = new Rectangle(h.getX() + 370 * getScale(), h.getY() + h.getHeight() - height, width, height);	
			} else {
				hitBox = new Rectangle((int)(h.getX() - 460 * getScale()), h.getY() + h.getHeight() - height, width, height);	
			}
		} 
		return hitBox;
	}
	
	@Override
	public boolean collidesOnAttack() {
		return false;
	}

	@Override
	public int getDuration() {
		return 600;
	}
	
	@Override
	public void onRelease() {
	}
}
